package com.mfhcd.facepay.utils;

import android.text.InputFilter;
import android.text.TextUtils;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: 石强
 * @E-Mail: shiqiang@mfhcd.com
 * @Date: 2019-11-05 10:47
 * @版权所有 © 现代金融控股（成都）有限公司
 * @描述:
 */
public class EditTextUtils {
    /**
     * 禁止EditText输入特殊字符
     *
     * @param editText     输入view
     * @param isAllowSpace 是否允许输入空格
     */
    public static void setEditTextInhibitInputSpeChat(EditText editText, boolean isAllowSpace) {
        InputFilter filter = (source, start, end, dest, dstart, dend) -> {
            String speChat = "[`~!@#$%^&*()+=|{}':;',\\[\\]<>/?~！@#￥%……&*（）\\-——+|{}【】‘；：”“’。，、？]";
            Pattern pattern = Pattern.compile(speChat);
            Matcher matcher = pattern.matcher(source.toString());
            if (isAllowSpace) {
                if (matcher.find()) {
                    return "";
                } else {
                    return null;
                }
            } else {
                if (matcher.find() || " ".equals(source.toString())) {
                    return "";
                } else {
                    return null;
                }
            }
        };
        //保留原有的exittext的filter
        InputFilter[] oldFilters = editText.getFilters();
        int oldFiltersLength = oldFilters.length;
        InputFilter[] newFilters = new InputFilter[oldFiltersLength + 1];
        if (oldFiltersLength > 0) {
            System.arraycopy(oldFilters, 0, newFilters, 0, oldFiltersLength);
        }
        //添加新的过滤规则
        newFilters[oldFiltersLength] = filter;
        editText.setFilters(newFilters);
    }

    public static boolean isEmpty(EditText... editTexts) {
        for (EditText editText : editTexts) {
            if (TextUtils.isEmpty(editText.getText())) {
                ToastUtil.showToast(editText.getHint().toString());
                return false;
            }
        }
        return true;
    }

    public static boolean isLegalName(String name) {
        boolean bool = RxRegTool.isLegalName(name);
        if (!bool)
            ToastUtil.showToast("请输入正确的姓名");
        return bool;
    }

    public static boolean isLegalPhone(String name) {
        boolean bool = RxRegTool.isMobile(name);
        if (!bool)
            ToastUtil.showToast("请输入正确的手机号");
        return bool;
    }

    public static boolean isLegalIdCard(String name) {
        boolean bool = RxRegTool.validateIdCard(name);
        if (!bool)
            ToastUtil.showToast("请输入正确的身份证号");
        return bool;
    }

    public static boolean isLegalBankCard(String name) {
        boolean bool = RxRegTool.isBankCard(name);
        if (!bool)
            ToastUtil.showToast("请输入正确的银行卡号");
        return bool;
    }
}
